package cn.qingfeng.a003_chat_v2.server.handler;

import cn.qingfeng.a003_chat_v2.message.GsendRequestMessage;
import cn.qingfeng.a003_chat_v2.server.groupsession.GroupSession;
import cn.qingfeng.a003_chat_v2.server.groupsession.GroupSessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.List;

@ChannelHandler.Sharable
public class GsendHandler extends SimpleChannelInboundHandler<GsendRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GsendRequestMessage msg) throws Exception {
        if (msg == null) {
            return;
        }

        String fromUsername = msg.getFromUsername();
        String groupName = msg.getGroupName();
        String content = msg.getContent();
        List<Channel> channels = GroupSessionFactory.getSession().getMembersChannel(groupName);
        for (Channel channel : channels) {
            channel.writeAndFlush(fromUsername + ":" + content);
        }
    }
}